package com.tanhua.server.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.mapper.QuestionMapper;
import com.tanhua.common.pojo.Question;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    //通过id查询到question
    public Question queryQuestion(Long userId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        return questionMapper.selectOne(queryWrapper);
    }


    public void saveQuestion(Long id, String content) {
        Question question = queryQuestion(id);
        //查询之前设置的问题，如果有就进行修改，如果没有就新增
        if(ObjectUtil.isNotEmpty(question)){
            question.setTxt(content);
            questionMapper.updateById(question);
        }else {
            question = new Question();
            question.setUserId(id);
            question.setTxt(content);
            question.setCreated(new Date());
            question.setUpdated(question.getCreated());
            questionMapper.insert(question);
        }
    }
}
